<template>
  <div>
    <!-- 1.登录头部导航提示 -->
    <van-nav-bar title="注册">
      <template #left>
        <van-icon name="arrow-left" size="18" @click="$router.back()" />
      </template>
    </van-nav-bar>

    <!-- 表单内容 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.nickname"
        name="nickname"
        label="昵称"
        placeholder="昵称"
        :rules="FormRules.nickname"
      />
      <van-field
        v-model.number="userInfo.mobile"
        type="text"
        name="mobile"
        label="手机号"
        placeholder="手机号"
        :rules="FormRules.mobild"
      />

      <van-field
        v-model="userInfo.password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="FormRules.password"
      />
      <van-field
        v-model="userInfo.repassword"
        type="repassword"
        name="repassword"
        label="确认密码"
        placeholder="确认密码"
        :rules="FormRules.repassword"
      />

      <van-field name="avatar" label="请选择头像">
        <template #input>
          <van-uploader
            v-model="userInfo.avatar"
            multiple
            :max-count="1"
            :after-read="afterRead"
          />
        </template>
      </van-field>

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { registry } from "@/api/login.js";
export default {
  name: "RegistryIndex",
  data() {
    const validatePassword = () => {
      return this.userInfo.password === this.userInfo.repassword;
    };

    return {
      userInfo: {
        nickname: "",
        mobile: "",
        password: "",
        repassword: "",
        avatar: [],
        currentUrl: "",
      },
      FormRules: {
        nickname: [
          { required: true, message: "请填写昵称" },
          { pattern: /^[^\s]{1,6}$/, message: "昵称不能用空格!" },
        ],
        mobile: [
          { required: true, message: "请填写密码" },
          { pattern: /^1[3-9]\d{9}$/, message: "手机格式不正确" },
        ],
        password: [
          { required: true, message: "请填写用户名" },
          { pattern: /^[a-zA-Z0-9]{3,9}$/, message: "用户名格式不正确" },
        ],
        repassword: [
          { required: true, message: "请填写密码" },
          { validator: validatePassword, message: "和原密码不一致" },
        ],
      },
      num: 0,
    };
  },
  methods: {
    async onSubmit(values) {
      // console.log("submit", values);
      try {
        const res = await registry(values);
        if (res.data.data === "注册成功") {
          this.$toast.success("注册成功!");
          this.$router.push("/login");
        } else {
          this.$toase.fail("注册失败,请重试!");
        }
      } catch (err) {
        // console.log(err.message);
        this.$toast.fail("发送请求失败!");
        console.log(err.message);
      }
    },
    // 上传完成 触发这个方法
    afterRead(file) {
      console.log(this.num++);
      // console.log(file.content);
      this.userInfo.avatar = [file];
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .van-nav-bar__content {
  background-color: rgb(225, 28, 52);
}
::v-deep .van-icon-arrow-left,
::v-deep .van-nav-bar__title {
  color: #fff;
}
// 225, 28, 52
</style>
